#ifndef _C_C_MEMORY_H_INCLUDED_
#define _C_C_MEMORY_H_INCLUDED_

#pragma once
#include "JeayeSDK.h"
#include "c_debug.h"
#include <stdlib.h>  
//#define __FILE_LINE_FUNCTION__ (_T(__FILE__)),((const INT32)__LINE__),(_T(__FUNCDNAME__))

#ifdef _DEBUG
#ifndef CC_DEBUG_MEMORY
#define CC_DEBUG_MEMORY
#endif
#endif /*ndef CC_DEBUG*/

/**/
#define C_MALLOCX(T,C) ((T*)c_malloc(sizeof(T) * (C)))
#define C_MALLOC(T) ((T*)c_malloc(sizeof(T)))
#define C_CALLOC(T,C) ((T*)c_calloc(C,sizeof(T)))
#define C_FREE(d) c_free(d)

/**/
#ifdef CC_DEBUG_MEMORY

/**/
#define c_calloc(c,d) c_safe_calloc((c),(d),(__FILE__),((const INT32)__LINE__),(__FUNCDNAME__))
/**/
#define c_malloc(d) c_safe_malloc((d),(__FILE__),((const INT32)__LINE__),(__FUNCDNAME__))
/**/
#define c_realloc(d,s) c_safe_realloc((d),(s),(__FILE__),((const INT32)__LINE__),(__FUNCDNAME__))

/**/
#define c_free(d) __safe_freeEx((d),(__FILE__),((const INT32)__LINE__),(__FUNCDNAME__))

/**/
#ifdef _UNICODE
#define c_tcsdup c_wcsdup
#else
#define c_tcsdup c_acsdup
#endif

#define c_wcsdup(d) c_safe_wcsdup((d),(__FILE__),((const INT32)__LINE__),(__FUNCDNAME__))
#define c_acsdup(d)	c_safe_acsdup((d),(__FILE__),((const INT32)__LINE__),(__FUNCDNAME__))

/**/
JEAYESDK_API void c_init_memory_debug(void);
/**/
JEAYESDK_API void c_print_memory_debug(char *filename);

JEAYESDK_API void c_safe_destroy(void * dat, const char * file, const int line, const char * func);
/**/
JEAYESDK_API void* c_safe_calloc(size_t, size_t, const char*, const int, const char*);
/**/
JEAYESDK_API void* c_safe_malloc(size_t, const char*, const int, const char*);
/**/
JEAYESDK_API void* c_safe_realloc(void*, size_t, const char*, const int, const char*);
/**/
JEAYESDK_API void __safe_freeEx(void * dat, const char * file, const int line, const char * func);
/**/
JEAYESDK_API char* c_safe_acsdup(const char*, const char*, const int, const char*);
/**/
JEAYESDK_API wchar_t* c_safe_wcsdup(const wchar_t*, const char*, const int, const char*);

/*************************************************************/
void * operator new(size_t size, const char * file, int line);
void operator delete(void * p, const char * file, int line);
void operator delete(void * p);
void operator delete[](void * p);

#else /*ELSE CC_DEBUG_MEMORY*/

/**/
JEAYESDK_API void* c_malloc(size_t);
/**/
JEAYESDK_API void* c_calloc(size_t, size_t);
/**/
JEAYESDK_API void* c_realloc(void*, size_t);
/**/
JEAYESDK_API void c_free(void*);

JEAYESDK_API char* c_acsdup(const char* str);
JEAYESDK_API wchar_t* c_wcsdup(const wchar_t*);

/**/
#ifdef _UNICODE
#define c_tcsdup(d) c_wcsdup(d)
#else
#define c_tcsdup(d) c_acsdup(d)
#endif

/**/
#define c_init_memory_debug() (void)0
/**/
#define c_print_memory_debug(x) (void)0

#endif /*END CC_DEBUG_MEMORY*/

#endif //_C_CC_MEMORY_H_INCLUDED_